package com.zbbt.sportslottery.ui.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import android.R.integer;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.bean.BallDataBean;
import com.zbbt.sportslottery.util.TimeUtil;

public class DrawNoticeAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<BallDataBean> awardnotice;
	public String state;
	// HashMap<Integer, View> lmap = new HashMap<Integer, View>();
	final int top = 1;
	final int center = 2;
	final int bottom = 3;
	int line;
    private Context myContext;
	public DrawNoticeAdapter(Context context, List<BallDataBean> awardnotice) {
		this.awardnotice = awardnotice;
		inflater = LayoutInflater.from(context);
		myContext=context;
	}

	@Override
	public int getCount() {
		return awardnotice.size();
	}

	public int getViewTypeCount() {
		return 3;
	}

	@Override
	public Object getItem(int position) {
		return awardnotice.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		if (awardnotice.get(position).getLotteryType() == 2) {
			return top;
		} else if (awardnotice.get(position).getLotteryType() == 3) {
			return center;
		} else if (awardnotice.get(position).getLotteryType() == 3) {
			return bottom;
		} else {
			return 0;
		}
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		View type1 = null;
		// BasketballViewHolder basketball = null;
		View type2 = null;
		int type = getItemViewType(position);
		final BallDataBean entity = awardnotice.get(position);
		switch (type) {
		case top:
			type1 = view;
			ViewHolder item1;
			if (type1 == null) {
				type1 = inflater.inflate(R.layout.item_award_notice_itop, null);
				TextView ball0;
				TextView ball1;
				TextView ball2;
				TextView ball3;
				TextView ball4;
				TextView ball5;
				TextView ball6;
				TextView ball7;
				item1 = new ViewHolder();
				item1.awardtype = (TextView) type1.findViewById(R.id.top_text);
				item1.phase = (TextView) type1.findViewById(R.id.top_phase);
				item1.date = (TextView) type1.findViewById(R.id.top_date);
				// vh.awarditem=(RelativeLayout)view.findViewById(R.id.award_item);
				ball0 = (TextView) type1.findViewById(R.id.top_ball0);
				ball1 = (TextView) type1.findViewById(R.id.top_ball1);
				ball2 = (TextView) type1.findViewById(R.id.top_ball2);
				ball3 = (TextView) type1.findViewById(R.id.top_ball3);
				ball4 = (TextView) type1.findViewById(R.id.top_ball4);
				ball5 = (TextView) type1.findViewById(R.id.top_ball5);
				ball6 = (TextView) type1.findViewById(R.id.top_ball6);
				ball7 = (TextView) type1.findViewById(R.id.top_ball7);

				item1.ball.add(ball0);
				item1.ball.add(ball1);
				item1.ball.add(ball2);
				item1.ball.add(ball3);
				item1.ball.add(ball4);
				item1.ball.add(ball5);
				item1.ball.add(ball6);
				item1.ball.add(ball7);
				type1.setTag(item1);
			} else {
				item1 = (ViewHolder) type1.getTag();
			}
			for (int i = 0; i < 8; i++) {
				item1.ball.get(i).setBackgroundColor(myContext.getResources().getColor(R.color.none));
				item1.ball.get(i).setText("");
			}
			item1.awardtype.setText(entity.getLotteryName());
			item1.phase.setText("第" + entity.getDrawId() + "期");
			item1.date.setText(TimeUtil.getTimeDate(entity.getResultTime()));
			String[] rednumb = entity.getResult().split(",");
			if (entity.getLotteryCode().equals("4707")) {
				Arrays.sort(rednumb); // 进行排序
			}
			state = entity.getDrawId();// 告诉 历史列表 我要加载哪个 球的adapter
			String[] bluenumb = null;
			if (!TextUtils.isEmpty(entity.getAddResult()) && !entity.getAddResult().equals("null")) {
				bluenumb = entity.getAddResult().split(",");
				if (entity.getLotteryCode().equals("4707")) {
					Arrays.sort(bluenumb); // 进行排序
				}
				for (int i = 0; i < (rednumb.length + bluenumb.length); i++) {
					if (i < rednumb.length) {
						item1.ball.get(i).setBackgroundResource(R.drawable.redball);
						item1.ball.get(i).setText(rednumb[i]);
					} else {
						if (bluenumb == null) {
							break;

						} else {
							item1.ball.get(i).setBackgroundResource(R.drawable.blueball);
							item1.ball.get(i).setText(bluenumb[i - rednumb.length]);
						}
					}
				}
			} else {
				for (int i = 0; i < rednumb.length; i++) {
					item1.ball.get(i).setBackgroundResource(R.drawable.redball);
					item1.ball.get(i).setText(rednumb[i]);
				}
			}
			view = type1;
			break;
		case center:
			type2 = view;
			winViewHolder item2;
			if (type2 == null) {
				type2 = inflater.inflate(R.layout.item_win, null);
				item2 = new winViewHolder();
				TextView win0;
				TextView win1;
				TextView win2;
				TextView win3;
				TextView win4;
				TextView win5;
				TextView win6;
				TextView win7;
				TextView win8;
				TextView win9;
				TextView win10;
				TextView win11;
				TextView win12;
				TextView win13;
				item2.awardtype = (TextView) type2.findViewById(R.id.win_text);
				item2.phase = (TextView) type2.findViewById(R.id.win_phase);
				item2.date = (TextView) type2.findViewById(R.id.win_date);
				win0 = (TextView) type2.findViewById(R.id.win_greenwin0);
				win1 = (TextView) type2.findViewById(R.id.win_greenwin1);
				win2 = (TextView) type2.findViewById(R.id.win_greenwin2);
				win3 = (TextView) type2.findViewById(R.id.win_greenwin3);
				win4 = (TextView) type2.findViewById(R.id.win_greenwin4);
				win5 = (TextView) type2.findViewById(R.id.win_greenwin5);
				win6 = (TextView) type2.findViewById(R.id.win_greenwin6);
				win7 = (TextView) type2.findViewById(R.id.win_greenwin7);
				win8 = (TextView) type2.findViewById(R.id.win_greenwin8);
				win9 = (TextView) type2.findViewById(R.id.win_greenwin9);
				win10 = (TextView) type2.findViewById(R.id.win_greenwin10);
				win11 = (TextView) type2.findViewById(R.id.win_greenwin11);
				win12 = (TextView) type2.findViewById(R.id.win_greenwin12);
				win13 = (TextView) type2.findViewById(R.id.win_greenwin13);
				item2.list.add(win0);
				item2.list.add(win1);
				item2.list.add(win2);
				item2.list.add(win3);
				item2.list.add(win4);
				item2.list.add(win5);
				item2.list.add(win6);
				item2.list.add(win7);
				item2.list.add(win8);
				item2.list.add(win9);
				item2.list.add(win10);
				item2.list.add(win11);
				item2.list.add(win12);
				item2.list.add(win13);
				type2.setTag(item2);
			} else {
				item2 = (winViewHolder) type2.getTag();
			}
			item2.awardtype.setText(entity.getLotteryName());
			item2.date.setText(TimeUtil.getTimeDate(entity.getResultTime()));
			item2.phase.setText("第" + entity.getDrawId() + "期");
			item2.date.setSelected(true);
			item2.phase.setSelected(true);
			String[] winball = entity.getResult().split(",");
			for (int i = 0; i < winball.length; i++) {
				item2.list.get(i).setText(winball[i]);
				item2.list.get(i).setBackgroundResource(R.drawable.green);
			}
			view = type2;
			break;
		// case bottom:
		// view = inflater.inflate(R.layout.item_award_notice_basketball, null);
		// basketball = new BasketballViewHolder();
		// basketball.awardtype = (TextView)
		// view.findViewById(R.id.bottom_text);
		// // basketball.phase=(TextView)view.findViewById(R.id.win_phase);
		// basketball.date = (TextView) view.findViewById(R.id.bottom_date);
		//
		// basketball.awardtype.setText(entity.getLotteryName());
		// // basketball.phase.setText(entity.getDraw_id());
		// basketball.date.setText(TimeUtil.getTimeDate(entity.getResultTime()));
		// basketball.url = entity.getResult();
		// state = basketball.url;
		// lmap.put(position, view);
		// view.setTag(basketball);
		// break;
		}
		return view;
	}

	static class ViewHolder {
		public TextView awardtype;
		public TextView phase;
		public TextView date;
		public List<TextView> ball = new ArrayList<TextView>();

	}

	static class BasketballViewHolder {
		public TextView awardtype;
		public TextView date;
		public String url;
	}

	static class winViewHolder {
		public TextView awardtype;
		public TextView phase;
		public TextView date;
		public List<TextView> list = new ArrayList<TextView>();
	}

}
